﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using YsTiffConverter.DataModel;
using SWF = System.Windows.Forms;

namespace YsTiffConverter.ViewModel
{
    /// <summary>
    /// Holds the main window data.
    /// </summary>
    internal class MainWindowViewModel
    {
        #region Fields

        private EditionCommand commandOpen_;
        private EditionCommand commandPrevious_;
        private EditionCommand commandNext_;
        private EditionCommand commandSaveAsJpeg_;
        private EditionCommand commandSaveAllAsJpeg_;
        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets or sets the tiff image instance.
        /// </summary>
        public TiffImage TiffImage
        {
            get; set;
        }

        public ICommand CommandOpen
        {
            get
            {
                if (commandOpen_ == null)
                {
                    commandOpen_ = new EditionCommand();
                    commandOpen_.OnCanExecute += CommandOpenOnCanExecute;
                    commandOpen_.OnExecute += CommandOpenOnExecute;
                }
                return commandOpen_;
            }
        }

        public ICommand CommandPrevious
        {
            get
            {
                if (commandPrevious_ == null)
                {
                    commandPrevious_ = new EditionCommand();
                    commandPrevious_.OnCanExecute += CommandPreviousOnCanExecute;
                    commandPrevious_.OnExecute += CommandPreviousOnExecute;
                }
                return commandPrevious_;
            }
        }

        public ICommand CommandNext
        {
            get
            {
                if (commandNext_ == null)
                {
                    commandNext_ = new EditionCommand();
                    commandNext_.OnCanExecute += CommandNextOnCanExecute;
                    commandNext_.OnExecute += CommandNextOnExecute;
                }
                return commandNext_;
            }
        }

        public ICommand CommandSaveAsJpeg
        {
            get
            {
                if (commandSaveAsJpeg_ == null)
                {
                    commandSaveAsJpeg_ = new EditionCommand();
                    commandSaveAsJpeg_.OnCanExecute += CommandSaveAsJpegOnCanExecute;
                    commandSaveAsJpeg_.OnExecute += CommandSaveAsJpegOnExecute;
                }
                return commandSaveAsJpeg_;
            }
        }

        public ICommand CommandSaveAllAsJpeg
        {
            get
            {
                if (commandSaveAllAsJpeg_ == null)
                {
                    commandSaveAllAsJpeg_ = new EditionCommand();
                    commandSaveAllAsJpeg_.OnCanExecute += CommandSaveAsJpegOnCanExecute;
                    commandSaveAllAsJpeg_.OnExecute += CommandSaveAllAsJpegOnExecute;
                }
                return commandSaveAllAsJpeg_;
            }
        }
        #endregion Properties

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the MainWindowViewModel class.
        /// </summary>
        public MainWindowViewModel()
        {
            TiffImage = new TiffImage();
        }

        #endregion Constructors

        #region Methods

        private void CommandOpenOnExecute(object parameter)
        {
            var openFileDialog = new SWF.OpenFileDialog
            {
                DefaultExt = ".tif",
                Filter = "TIFF images (*.tif;*.tiff)|*.tif;*.tiff",
                Title = "Select image"
            };

            if (openFileDialog.ShowDialog() == SWF.DialogResult.OK)
            {
                TiffImage.FileName = openFileDialog.FileName;
                string errorText;

                if (!TiffImage.Open(TiffImage.FileName, out errorText))
                {
                    MessageBox.Show(errorText, "Open image file", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        private static bool CommandOpenOnCanExecute(object parameter)
        {
            return true;
        }

        public void CommandPreviousOnExecute(object parameter)
        {
            TiffImage.PreviousFrame();
        }

        private bool CommandPreviousOnCanExecute(object parameter)
        {
            return (TiffImage.CurrentFrameNumber > 0);
        }

        private void CommandNextOnExecute(object parameter)
        {
            TiffImage.NextFrame();
        }

        private bool CommandNextOnCanExecute(object parameter)
        {
            return (TiffImage.CurrentFrameNumber < (TiffImage.FrameCount - 1));
        }

        private void CommandSaveAsJpegOnExecute(object parameter)
        {
            var fileName = String.Format("{0}-Page-{1}.jpg", Path.GetFileNameWithoutExtension(TiffImage.FileName),
                                         TiffImage.ImagePage);

            var saveFileDialog = new SWF.SaveFileDialog
                                     {
                                         FileName = fileName,
                                         DefaultExt = ".jpg",
                                         Filter = "JPEG images (*.jpg)|*.jpg",
                                         Title = "Save image"
                                     };
            if (saveFileDialog.ShowDialog() == SWF.DialogResult.OK)
            {
                using (var fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create))
                {
                    var encoder = new JpegBitmapEncoder
                                      {
                                          QualityLevel = 100
                                      };
                    encoder.Frames.Add(TiffImage.CurrentFrame);
                    encoder.Save(fileStream);
                }
            }
        }

        private bool CommandSaveAsJpegOnCanExecute(object parameter)
        {
            return (TiffImage.CurrentFrame != null);
        }

        private void CommandSaveAllAsJpegOnExecute(object parameter)
        {
            var saveAllFramesWindow = new SaveAllFramesWindow(this.TiffImage.FileName);
            saveAllFramesWindow.ShowDialog();
        }

        #endregion Methods
    }
}
